/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package au.edu.swin.unilearn;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateful;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.context.FacesContext;

/**
 *
 * @author Coquinone
 */
@Stateful
public class MarkingSchemeDetails
{
    private String title;
    private List<String> detailsList;
    private String allocatedMark;
    private int mark;

    public MarkingSchemeDetails(String title, String allocatedMark)
    {
        this.title = title;
        this.allocatedMark = allocatedMark;
        detailsList = new ArrayList();
    }

    public String getTitle()
    {
        return title;
    }

    public void setTitle(String title)
    {
        this.title = title;
    }

    public List<String> getDetailsList()
    {
        return detailsList;
    }

    public void setDetailsList(List<String> detailsList)
    {
        this.detailsList = detailsList;
    }

    public String getAllocatedMark()
    {
        return allocatedMark;
    }

    public void setAllocatedMark(String allocatedMark)
    {
        this.allocatedMark = allocatedMark;
    }

    public void addDetails(String details)
    {
        detailsList.add(details);
    }

    public int getMark()
    {
        return mark;
    }

    public void setMark(int mark)
    {
        this.mark = mark;
    }
    
    public int writeDetails(PrintWriter out)
    {
        out.println(title);
        for(String str : detailsList)
        {
            out.println("- " + str);
        }
        out.println();
        out.println("Mark Received: " + mark + "/" + allocatedMark);
        out.println("----------------------------------------------------------------");
        return mark;
    }
    
    public void validateInputMark(FacesContext context, UIComponent toValidate, Object obj)
    {
        int givenMark = ((Integer) obj).intValue();
        if(givenMark > (new Integer(allocatedMark)).intValue())
        {
            ((UIInput) toValidate).setValid(false);
            FacesMessage message = new FacesMessage("Mark given must be less than " + allocatedMark);
            context.addMessage(toValidate.getClientId(context), message);
        }
    }
    
    public void resetMark()
    {
        mark = 0;
    }
}
